<?php

/**
 * This is the model class for table "config".
 *
 * The followings are the available columns in table 'config':
 * @property string $id
 * @property string $group_id
 * @property string $name
 * @property string $value
 * @property string $html_tag
 */
class Config extends ConfigPeer
{
	var $className = __CLASS__;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return Config the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

    /**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('group_id, name, value, html_tag', 'required'),
			array('group_id', 'length', 'max'=>10),
			array('name', 'length', 'max'=>45),
            array('name', 'specialCharacter'),
            array('name', 'checkExists'),
			array('value', 'length', 'max'=>300),
			array('html_tag', 'length', 'max'=>100),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, group_id, name, value, html_tag', 'safe', 'on'=>'search'),
		);
	}

    public function specialCharacter($attribute, $params){
        if (!preg_match('/^[a-zA-Z0-9_]+$/', $this->name)) {
            $this->addError('name',Yii::t('BackEnd', 'Config name is not contained the special character and space.'));
        }
    }

    public function checkExists($attribute, $params){
        if(!$this->id > 0){
            $config = Config::model()->findByAttributes(array('name'=>$this->name, 'group_id'=>$this->group_id));
            if($config){
                $this->addError('name',Yii::t('BackEnd', 'Config name was existed.'));
            }
        }
    }

    public function getAllConfig($groupID)
    {
        $critial = new CDbCriteria();
        $critial->condition = "group_id = $groupID";
        $critial->order = "name, id";
        return $configs = Config::model()->findAll($critial);
    }
}